package com.xzp.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xzp.yygh.cmn.client.DictFeignClient;
import com.xzp.yygh.enums.DictEnum;
import com.xzp.yygh.model.user.Patient;
import com.xzp.yygh.user.mapper.PatientMapper;
import com.xzp.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 根据userId找到所有绑定的就诊人
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllUserId(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        patientList.stream().forEach(item->{
            this.packPatient(item);
        });
        return patientList;
    }


    /**
     * 根据id获取就诊人的详细信息
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }


    /**
     * 通过数据字典获得就诊人code对应的字段
     * @param patient
     * @return
     */
    private Patient packPatient(Patient patient){
        String certificatesType = patient.getCertificatesType();
        // 根据dictcode和certificatesType获取证件类型
        String certificatesTypeString = dictFeignClient.getName((DictEnum.CERTIFICATES_TYPE.getDictCode()), certificatesType);
        // 省
        String provinceCode = patient.getProvinceCode();
        String provinceCodeString = dictFeignClient.getName(provinceCode);
        // 市
        String cityCode = patient.getCityCode();
        String cityCodeString = dictFeignClient.getName(cityCode);
        // 区
        String districtCode = patient.getDistrictCode();
        String districtCodeString = dictFeignClient.getName(districtCode);
        // 联系人证件类型(非必填)
        String contactsCertificatesType = patient.getContactsCertificatesType();
        if (!StringUtils.isEmpty(contactsCertificatesType)) {
            String contactsCertificatesTypeString = dictFeignClient.getName(contactsCertificatesType);
            patient.getParam().put("contactsCertificatesType", contactsCertificatesTypeString);
        }
        // 详细地址
        String fullAddress = provinceCodeString + cityCodeString + districtCodeString + patient.getAddress();

        // 放入map
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("provinceString", provinceCodeString);
        patient.getParam().put("cityString", cityCodeString);
        patient.getParam().put("districtString", districtCodeString);
        patient.getParam().put("address", fullAddress);

        return patient;





    }
}
